package 周赛._283;

import java.util.*;

public class _6017_向数组中追加K个整数 {
    public static void main(String[] args) {
        int[] nums = new int[]{96,44,99,25,61,84,88,18,19,33,60,86,52,19,32,47,35,50,94,17,29,98,22,21,72,100,40,84};
        long l = minimalKSum(nums, 35);
        System.out.println(l);
    }
    public static long minimalKSum(int[] nums, int k) {
        Set<Integer> hash = new HashSet<>();
        long x = k;
        long sum = (x * (1 + x)) >> 1 ;
        Arrays.sort(nums);
        for (int num : nums) {
            if (!hash.contains(num)) {
                if (num <= x) {
                    sum -= num;
                    sum += (++x);
                    hash.add(num);
                } else {
                    break;
                }
            }
        }
        return sum;
    }
}
